<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
use think\facade\Session;
// use app\admin\Model\Gkdata;
class Xsuser extends Base
{
    // 搜索名称查用户
	public function search(){
        $request=new request();
        $sname=$request->post('sname');
        $data=Db::table('xsname')->where('xsname','like','%'.$sname.'%')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function index()
    {
        $data=Db::table('xsuser')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 进入添加用户页面
    public function addpage(){
        return $this->fetch();
    }
    // 添加用户
    public  function addad(){
        // $adxm1=Session::get('adxm');
        $request=new request();
        $xsname = $request->post("xsname");
        $xsphone = $request->post("xsphone");
        $sex = $request->post("sex");
        // 实例化上传图片的对象
        $upimgobj=controller('Common');
        $image_error = $_FILES['xsimg']['error'];
        if ($image_error == 4) {
            $xsimg='';
        }else{
            $file=$request->file('xsimg');
            $xsimg=$upimgobj->upaction1($file);          
        }
        $data1=[
            "xsname"=>$xsname,
            "xsphone"=>$xsphone,
            "xsimg"=>$xsimg,
            "sex"=>$sex
        ];
        $re=Db::name('xsuser')->insert($data1);
        $xsid = Db::name('xsuser')->getLastInsID();
        if($re){
            $codeimg=$this->getwxcode($xsid);
            // var_dump($codeimg);
            if(!empty($codeimg)){
                // $imgdata=$upimgobj->upaction1($file);
               
                    $updata=[
                        "xsid"=>$xsid,
                        "xscode"=>$codeimg,
                        ];
                    $upda=Db::name('xsuser')->update($updata);
                    if($upda){
                        $this->success('新增用户成功,二维码生成成功', 'xsuser/index');
                    }else{
                        return $this->error('新增用户成功,但二维码生成失败！请联系技术人员');
                    }
                
            }else{
                $this->success('新增用户成功,但二维码生成失败！请联系技术人员', 'xsuser/index');
            }
        }else{
            return $this->error('新增用户失败');
        }
            
            
        
    }
    // 进入更改
    public function editpage(){
        $request=new request();
        $xsid= intval($request->get("id"));
        $data=Db::name('xsuser')->where('xsid',$xsid)->find();       
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 更改用户
    public  function edituser(){
        $request=new request();
        $data1=[];
        $xsid=$request->post('xsid');
        $data1['xsid']=$xsid;
        $data=Db::name('xsuser')->where('xsid',$xsid)->find();
        $xsname = $request->post("xsname");
        if($xsname!=$data['xsname']){
            $data1['xsname']=$xsname;
        }
        $xsphone = $request->post("xsphone");
        if($xsphone!=$data['xsphone']){
            $data1['xsphone']=$xsphone;
        }
        $sex = $request->post("sex");
        if($sex!=$data['sex']){
            $data1['sex']=$sex;
        }
        // 实例化上传图片的对象
        $upimgobj=controller('Common');
        $image_error = $_FILES['xsimg']['error'];
        if ($image_error != 4) {
            $file=$request->file('xsimg');
            $xsimg=$upimgobj->upaction1($file);
            $data1['xsimg']=$xsimg;
        }
        
        $da=Db::name('xsuser')->update($data1);
        if($da){
            $this->success('更新用户成功', 'xsuser/index');
        }else{
            return $this->error('更新用户失败');
        }

    }
    // 删除用户
    public function delxsuser()
    {

        $request=new request();
        $xsid=$request->get("id");
        $da=Db::name('xsuser')->delete($xsid);
        if($da){
            $this->success('删除成功', 'xsuser/index');
        }
        else{
            return $this->error('删除失败');
        }
    }
    // 获取二维码
    public function getwxcode($id){
        $appid="wx7cff958a038c1630";
        $secret="b54a30a68d3d3173743a24bd9f853901";
        $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        //缓存access_token
         Session::set('access_token','');
         Session::set('expires_in',0);
         $ACCESS_TOKEN ="";
         if(empty(Session::get('access_token')) || (!empty(Session::get('expires_in')) && time() >Session::get('expires_in')))
         {
             $json = $this->httpRequest($access_token );
             $json = json_decode($json,true);
             // var_dump($json);
             Session::set('access_token',$json['access_token']);
             Session::set('expires_in',time()+7200);
             $ACCESS_TOKEN =$json["access_token"];
         }
         else{
             $ACCESS_TOKEN = Session::get('access_token');
         }
        //  var_dump($ACCESS_TOKEN);
        //构建请求二维码参数
        //path是扫描二维码跳转的小程序路径，可以带参数?id=xxx
        //width是二维码宽度
        $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
        // $qcode ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$ACCESS_TOKEN";
        // "scene"=>'?goods_id='.$goods_id.'&store_id='.$store_id,
        $param = json_encode(array("path"=>"tuanz/index/index","width"=> 100));
        $data = [
            'scene'=>'?xsid='.$id,
            'width'=>80,
            'page'=>'tuanz/index/index',
            'auto_color'=>false,
        ];
        $data = json_encode($data); 
        
        //POST参数
        $result =$this->httpRequest($qcode,$data,"POST");
        //生成二维码
        file_put_contents("./style/wxcode/qrcode".$id.".png",$result);
        $base64_image ="data:image/jpeg;base64,".base64_encode($result );
        return "/style/wxcode/qrcode".$id.".png";
        
        
       
        
    }
    public function httpRequest($url,$data='',$method='GET'){
            $curl = curl_init(); 
            curl_setopt($curl, CURLOPT_URL,$url); 
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
            curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); 
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1); 
            if($method=='POST')
            {
                curl_setopt($curl, CURLOPT_POST, 1);
                if ($data !='')
                {
                    curl_setopt($curl, CURLOPT_POSTFIELDS,$data); 
                }
            }
         
            curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
            curl_setopt($curl, CURLOPT_HEADER, 0); 
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
            $result = curl_exec($curl); 
            curl_close($curl); 
            return $result;
    }



}
